﻿package com.hotau;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.TextView;

/**
 * @author fengweili 2011-4-7 上午09:37:32
 */
public class NavTabActivity extends TabActivity {
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.navpanel);
		final TabHost tabHost = getTabHost();
		TabHost.TabSpec spec;

		Intent intent = new Intent(this, HotauActivity.class);
		spec = tabHost.newTabSpec("hightService")
				.setIndicator(returnIndicator("高端服务", R.drawable.item0))
				.setContent(intent);
		tabHost.addTab(spec);

		intent = new Intent(this, HotauActivity.class);
		spec = tabHost.newTabSpec("hightService")
				.setIndicator(returnIndicator("高端服务", R.drawable.item1))
				.setContent(intent);
		tabHost.addTab(spec);

		intent = new Intent(this, HotauActivity.class);
		spec = tabHost.newTabSpec("hightService")
				.setIndicator(returnIndicator("高端服务", R.drawable.item2))
				.setContent(intent);
		tabHost.addTab(spec);

		intent = new Intent(this, HotauActivity.class);
		spec = tabHost.newTabSpec("hightService")
				.setIndicator(returnIndicator("高端服务", R.drawable.item3))
				.setContent(intent);
		tabHost.addTab(spec);

		tabHost.setOnTabChangedListener(new OnTabChangeListener() {

			public void onTabChanged(String tabId) {
				selecteTab(tabHost.getTabWidget(), tabHost.getCurrentTab());
			}
		});
		selecteTab(tabHost.getTabWidget(), 0);
	}

	public View returnIndicator(String title, int iconId) {
		View tabView = this.getLayoutInflater().inflate(R.layout.tabitem, null);
		TextView textView = (TextView) tabView.findViewById(R.id.tabText);
		textView.setText(title);
		textView.setSingleLine();
		textView.setTextSize(10);
		textView.setTextColor(Color.GRAY);
		ImageView imageView = (ImageView) tabView.findViewById(R.id.tabIcon);
		imageView.setImageDrawable(getResources().getDrawable(iconId));
		return tabView;
	}

	public void selecteTab(TabWidget tabWidget, int selectedTab) {
		tabWidget.setCurrentTab(selectedTab);
		int tabCount = tabWidget.getTabCount();
		for (int i = 0; i < tabCount; i++) {
			View tabView = tabWidget.getChildTabViewAt(i);
			TextView textView = (TextView) tabView.findViewById(R.id.tabText);
			textView.setTextColor(Color.GRAY);
		}
		TextView selectedTextView = (TextView) tabWidget.getChildTabViewAt(
				selectedTab).findViewById(R.id.tabText);
		selectedTextView.setTextColor(Color.WHITE);

		// 隐藏软软键盘
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(tabWidget.getChildTabViewAt(selectedTab)
				.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
	}

	/****************************************************************************************/

	private void hideIM(View edt) {

		try {
			InputMethodManager im = (InputMethodManager) this
					.getSystemService(Activity.INPUT_METHOD_SERVICE);
			IBinder windowToken = edt.getWindowToken();
			if (windowToken != null) {
				// always de-activate IM

				im.hideSoftInputFromWindow(windowToken, 0);
			}
		} catch (Exception e) {
			Log.e("HideInputMethod", "failed:" + e.getMessage());
		}
	}

	// @Override
	// public boolean onCreateOptionsMenu(Menu menu) {
	// MenuInflater menuInflater = getMenuInflater();
	// menuInflater.inflate(R.menu.test_menu, menu);
	// return true;
	// }
	//
	// @Override
	// public boolean onOptionsItemSelected(MenuItem item) {
	// String result;
	// try {
	// result = DataTransformer.readFromFile("data.txt");
	// DataTransformer.resultIntoDB(this, result);
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// return super.onOptionsItemSelected(item);
	// }

	public void hideNav() {
		this.findViewById(R.id.navContainer).setVisibility(View.GONE);
	}

	public void showNav() {
		this.findViewById(R.id.navContainer).setVisibility(View.VISIBLE);
	}

}
